package com.tis.interfaces.servlet;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class GetUrlTags extends SimpleTagSupport {	
	private static final long serialVersionUID = 1L;
	
	@Override 
	public void doTag() throws JspException, IOException {
		super.doTag();
		
		JspWriter out = getJspContext().getOut();
		try{
			
			JspFragment f = getJspBody();
			if(f != null) f.invoke(out);
			
			PageContext pageContext = (PageContext) getJspContext();  
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			
			String uri = request.getRequestURI();
			String scheme = request.getScheme();
			String serverName = request.getServerName();
			int portNumber = request.getServerPort();
			String contextPath = request.getContextPath();
			String servletPath = request.getServletPath();
			String pathInfo = request.getPathInfo();
			String potocal = request.getProtocol();
			
			String url = "http://" + serverName + ":" + portNumber;
			//System.out.println("get url =" + url);
			out.print(url);
			/*
			 * Url: http://localhost:8080/urlinfo
			 * Uri: /urlinfo
			 * Scheme: http
			 * Server Name: localhost
			 * Port: 8080
			 * Context Path: 
			 * Servlet Path: /urlinfo
			 * Path Info: null
			 * Query: x=1&y=1
			 * 
			 */			
		}catch(IOException ex){
			throw new JspException("Error in NameTagHandler tag", ex);
		}
	}
		
}
